home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_perl.idb / usr / freeware / catman / u_man / cat1 / perlfaq3.Z / perlfaq3
Encoding:
Text File  |  1998-10-28  |  38.4 KB  |  925 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))      5555////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.       perlfaq3 - Programming Tools ($Revision: 1.29    $, $Date:
  10.       1998/08/05 11:57:04 $)
  11.  
  12.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  13.       This section of the FAQ answers questions related to
  14.       programmer tools and programming support.
  15.  
  16.       HHHHoooowwww ddddoooo IIII ddddoooo ((((aaaannnnyyyytttthhhhiiiinnnngggg))))????
  17.  
  18.       Have you looked at CPAN (see the _p_e_r_l_f_a_q_2 manpage)?  The
  19.       chances are that someone has already written a module    that
  20.       can solve your problem.  Have    you read the appropriate man
  21.       pages?  Here's a brief index:
  22.  
  23.           Basics      perldata, perlvar, perlsyn, perlop, perlsub
  24.           Execution      perlrun, perldebug
  25.           Functions      perlfunc
  26.           Objects      perlref, perlmod, perlobj, perltie
  27.           Data Structures perlref, perllol, perldsc
  28.           Modules      perlmod, perlmodlib, perlsub
  29.           Regexps      perlre, perlfunc, perlop, perllocale
  30.           Moving to perl5 perltrap, perl
  31.           Linking w/C      perlxstut, perlxs, perlcall, perlguts, perlembed
  32.           Various      http://www.perl.com/CPAN/doc/FMTEYEWTK/index.html
  33.                   (not a man-page but still useful)
  34.  
  35.       the _p_e_r_l_t_o_c manpage provides a crude table of    contents for
  36.       the perl man page set.
  37.  
  38.       HHHHoooowwww ccccaaaannnn IIII uuuusssseeee    PPPPeeeerrrrllll iiiinnnntttteeeerrrraaaaccccttttiiiivvvveeeellllyyyy????
  39.  
  40.       The typical approach uses the    Perl debugger, described in
  41.       the _p_e_r_l_d_e_b_u_g(1) man page, on    an ``empty'' program, like
  42.       this:
  43.  
  44.           perl -de 42
  45.  
  46.       Now just type    in any legal Perl code,    and it will be
  47.       immediately evaluated.  You can also examine the symbol
  48.       table, get stack backtraces, check variable values, set
  49.       breakpoints, and other operations typically found in
  50.       symbolic debuggers.
  51.  
  52.       IIIIssss tttthhhheeeerrrreeee aaaa PPPPeeeerrrrllll sssshhhheeeellllllll????
  53.  
  54.       In general, no.  The Shell.pm    module (distributed with perl)
  55.       makes    perl try commands which    aren't part of the Perl
  56.       language as shell commands.  perlsh from the source
  57.       distribution is simplistic and uninteresting,    but may    still
  58.       be what you want.
  59.  
  60.  
  61.  
  62.  
  63.      Page 1                        (printed 10/23/98)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))      5555////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))
  71.  
  72.  
  73.  
  74.       HHHHoooowwww ddddoooo IIII ddddeeeebbbbuuuugggg mmmmyyyy PPPPeeeerrrrllll pppprrrrooooggggrrrraaaammmmssss????
  75.  
  76.       Have you used    -w?  It    enables    warnings for dubious
  77.       practices.
  78.  
  79.       Have you tried use strict?  It prevents you from using
  80.       symbolic references, makes you predeclare any    subroutines
  81.       that you call    as bare    words, and (probably most importantly)
  82.       forces you to    predeclare your    variables with my or use vars.
  83.  
  84.       Did you check    the returns of each and    every system call?
  85.       The operating    system (and thus Perl) tells you whether they
  86.       worked or not, and if    not why.
  87.  
  88.         open(FH, ">    /etc/cantwrite")
  89.           or die "Couldn't write to    /etc/cantwrite:    $!\n";
  90.  
  91.       Did you read the _p_e_r_l_t_r_a_p manpage?  It's full    of gotchas for
  92.       old and new Perl programmers,    and even has sections for
  93.       those    of you who are upgrading from languages    like _a_w_k and
  94.       _C.
  95.  
  96.       Have you tried the Perl debugger, described in the _p_e_r_l_d_e_b_u_g
  97.       manpage?  You    can step through your program and see what
  98.       it's doing and thus work out why what    it's doing isn't what
  99.       it should be doing.
  100.  
  101.       HHHHoooowwww ddddoooo IIII pppprrrrooooffffiiiilllleeee mmmmyyyy PPPPeeeerrrrllll pppprrrrooooggggrrrraaaammmmssss????
  102.  
  103.       You should get the Devel::DProf module from CPAN, and    also
  104.       use Benchmark.pm from    the standard distribution.  Benchmark
  105.       lets you time    specific portions of your code,    while
  106.       Devel::DProf gives detailed breakdowns of where your code
  107.       spends its time.
  108.  
  109.       Here's a sample use of Benchmark:
  110.  
  111.         use    Benchmark;
  112.  
  113.         @junk = `cat /etc/motd`;
  114.         $count = 10_000;
  115.  
  116.         timethese($count, {
  117.               'map' => sub { my    @a = @junk;
  118.                      map { s/a/b/ } @a;
  119.                      return @a
  120.                    },
  121.               'for' => sub { my    @a = @junk;
  122.                      local $_;
  123.                      for (@a) {    s/a/b/ };
  124.                      return @a },
  125.              });
  126.  
  127.  
  128.  
  129.      Page 2                        (printed 10/23/98)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))      5555////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))
  137.  
  138.  
  139.  
  140.       This is what it prints (on one machine--your results will be
  141.       dependent on your hardware, operating    system,    and the    load
  142.       on your machine):
  143.  
  144.         Benchmark: timing 10000 iterations of for, map...
  145.            for:     4 secs    ( 3.97 usr  0.01 sys =    3.98 cpu)
  146.            map:     6 secs    ( 4.97 usr  0.00 sys =    4.97 cpu)
  147.  
  148.  
  149.       HHHHoooowwww ddddoooo IIII ccccrrrroooossssssss----rrrreeeeffffeeeerrrreeeennnncccceeee mmmmyyyy PPPPeeeerrrrllll pppprrrrooooggggrrrraaaammmmssss????
  150.  
  151.       The B::Xref module, shipped with the new, alpha-release Perl
  152.       compiler (not    the general distribution prior to the 5.005
  153.       release), can    be used    to generate cross-reference reports
  154.       for Perl programs.
  155.  
  156.           perl -MO=Xref[,OPTIONS] scriptname.plx
  157.  
  158.  
  159.       IIIIssss tttthhhheeeerrrreeee aaaa pppprrrreeeettttttttyyyy----pppprrrriiiinnnntttteeeerrrr ((((ffffoooorrrrmmmmaaaatttttttteeeerrrr))))    ffffoooorrrr PPPPeeeerrrrllll????
  160.  
  161.       There    is no program that will    reformat Perl as much as
  162.       _i_n_d_e_n_t(1) does for C.     The complex feedback between the
  163.       scanner and the parser (this feedback    is what    confuses the
  164.       vgrind and emacs programs) makes it challenging at best to
  165.       write    a stand-alone Perl parser.
  166.  
  167.       Of course, if    you simply follow the guidelines in the
  168.       _p_e_r_l_s_t_y_l_e manpage, you shouldn't need    to reformat.  The
  169.       habit    of formatting your code    as you write it    will help
  170.       prevent bugs.     Your editor can and should help you with
  171.       this.     The perl-mode for emacs can provide a remarkable
  172.       amount of help with most (but    not all) code, and even    less
  173.       programmable editors can provide significant assistance.
  174.  
  175.       If you are used to using _v_g_r_i_n_d program for printing out
  176.       nice code to a laser printer,    you can    take a stab at this
  177.       using
  178.       http://www.perl.com/CPAN/doc/misc/tips/working.vgrind.entry,
  179.       but the results are not particularly satisfying for
  180.       sophisticated    code.
  181.  
  182.       IIIIssss tttthhhheeeerrrreeee aaaa ccccttttaaaaggggssss ffffoooorrrr PPPPeeeerrrrllll????
  183.  
  184.       There's a simple one at
  185.       http://www.perl.com/CPAN/authors/id/TOMC/scripts/ptags.gz
  186.       which    may do the trick.
  187.  
  188.       WWWWhhhheeeerrrreeee    ccccaaaannnn IIII ggggeeeetttt PPPPeeeerrrrllll mmmmaaaaccccrrrroooossss ffffoooorrrr vvvviiii????
  189.  
  190.       For a    complete version of Tom    Christiansen's vi
  191.       configuration    file, see
  192.  
  193.  
  194.  
  195.      Page 3                        (printed 10/23/98)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))      5555////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))
  203.  
  204.  
  205.  
  206.       http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/toms.exrc,
  207.       the standard benchmark file for vi emulators.     This runs
  208.       best with nvi, the current version of    vi out of Berkeley,
  209.       which    incidentally can be built with an embedded Perl
  210.       interpreter -- see http://www.perl.com/CPAN/src/misc.
  211.  
  212.       WWWWhhhheeeerrrreeee    ccccaaaannnn IIII ggggeeeetttt ppppeeeerrrrllll----mmmmooooddddeeee ffffoooorrrr    eeeemmmmaaaaccccssss????
  213.  
  214.       Since    Emacs version 19 patchlevel 22 or so, there have been
  215.       both a perl-mode.el and support for the perl debugger    built
  216.       in.  These should come with the standard Emacs 19
  217.       distribution.
  218.  
  219.       In the perl source directory,    you'll find a directory    called
  220.       "emacs", which contains a cperl-mode that color-codes
  221.       keywords, provides context-sensitive help, and other nifty
  222.       things.
  223.  
  224.       Note that the    perl-mode of emacs will    have fits with
  225.       "main'foo" (single quote), and mess up the indentation and
  226.       hilighting.  You should be using "main::foo" in new Perl
  227.       code anyway, so this shouldn't be an issue.
  228.  
  229.       HHHHoooowwww ccccaaaannnn IIII uuuusssseeee    ccccuuuurrrrsssseeeessss wwwwiiiitttthhhh PPPPeeeerrrrllll????
  230.  
  231.       The Curses module from CPAN provides a dynamically loadable
  232.       object module    interface to a curses library.    A small    demo
  233.       can be found at the directory
  234.       http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/rep;
  235.       this program repeats a command and updates the screen    as
  236.       needed, rendering rrrreeeepppp    ppppssss aaaaxxxxuuuu similar to ttttoooopppp.
  237.  
  238.       HHHHoooowwww ccccaaaannnn IIII uuuusssseeee    XXXX oooorrrr TTTTkkkk    wwwwiiiitttthhhh PPPPeeeerrrrllll????
  239.  
  240.       Tk is    a completely Perl-based, object-oriented interface to
  241.       the Tk toolkit that doesn't force you    to use Tcl just    to get
  242.       at Tk.  Sx is    an interface to    the Athena Widget set.    Both
  243.       are available    from CPAN.  See    the directory
  244.       http://www.perl.com/CPAN/modules/by-
  245.       category/08_User_Interfaces/
  246.  
  247.       Invaluable for Perl/Tk programming are: the Perl/Tk FAQ at
  248.       http://w4.lns.cornell.edu/~pvhp/ptk/ptkTOC.html , the
  249.       Perl/Tk Reference Guide available at
  250.       http://www.perl.com/CPAN-local/authors/Stephen_O_Lidie/ ,
  251.       and the online manpages at http://www-
  252.       users.cs.umn.edu/~amundson/perl/perltk/toc.html .
  253.  
  254.       HHHHoooowwww ccccaaaannnn IIII ggggeeeennnneeeerrrraaaatttteeee ssssiiiimmmmpppplllleeee mmmmeeeennnnuuuussss wwwwiiiitttthhhhoooouuuutttt uuuussssiiiinnnngggg    CCCCGGGGIIII oooorrrr TTTTkkkk????
  255.  
  256.       The
  257.       http://www.perl.com/CPAN/authors/id/SKUNZ/perlmenu.v4.0.tar.gz
  258.  
  259.  
  260.  
  261.      Page 4                        (printed 10/23/98)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))      5555////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))
  269.  
  270.  
  271.  
  272.       module, which    is curses-based, can help with this.
  273.  
  274.       WWWWhhhhaaaatttt iiiissss uuuunnnndddduuuummmmpppp????
  275.  
  276.       See the next questions.
  277.  
  278.       HHHHoooowwww ccccaaaannnn IIII mmmmaaaakkkkeeee mmmmyyyy PPPPeeeerrrrllll pppprrrrooooggggrrrraaaammmm rrrruuuunnnn ffffaaaasssstttteeeerrrr????
  279.  
  280.       The best way to do this is to    come up    with a better
  281.       algorithm.  This can often make a dramatic difference.
  282.       Chapter 8 in the Camel has some efficiency tips in it    you
  283.       might    want to    look at.  Jon Bentley's    book ``Programming
  284.       Pearls'' (that's not a misspelling!)    has some good tips on
  285.       optimization,    too.  Advice on    benchmarking boils down    to:
  286.       benchmark and    profile    to make    sure you're optimizing the
  287.       right    part, look for better algorithms instead of
  288.       microtuning your code, and when all else fails consider just
  289.       buying faster    hardware.
  290.  
  291.       A different approach is to autoload seldom-used Perl code.
  292.       See the AutoSplit and    AutoLoader modules in the standard
  293.       distribution for that.  Or you could locate the bottleneck
  294.       and think about writing just that part in C, the way we used
  295.       to take bottlenecks in C code    and write them in assembler.
  296.       Similar to rewriting in C is the use of modules that have
  297.       critical sections written in C (for instance,    the PDL    module
  298.       from CPAN).
  299.  
  300.       In some cases, it may    be worth it to use the backend
  301.       compiler to produce byte code    (saving    compilation time) or
  302.       compile into C, which    will certainly save compilation    time
  303.       and sometimes    a small    amount (but not    much) execution    time.
  304.       See the question about compiling your    Perl programs for more
  305.       on the compiler--the wins aren't as obvious as you'd hope.
  306.  
  307.       If you're currently linking your perl    executable to a    shared
  308.       _l_i_b_c._s_o, you can often gain a    10-25% performance benefit by
  309.       rebuilding it    to link    with a static libc.a instead.  This
  310.       will make a bigger perl executable, but your Perl programs
  311.       (and programmers) may    thank you for it.  See the _I_N_S_T_A_L_L
  312.       file in the source distribution for more information.
  313.  
  314.       Unsubstantiated reports allege that Perl interpreters    that
  315.       use sfio outperform those that don't (for IO intensive
  316.       applications).  To try this, see the _I_N_S_T_A_L_L file in the
  317.       source distribution, especially the ``Selecting File IO
  318.       mechanisms'' section.
  319.  
  320.       The undump program was an old    attempt    to speed up your Perl
  321.       program by storing the already-compiled form to disk.     This
  322.       is no    longer a viable    option,    as it only worked on a few
  323.       architectures, and wasn't a good solution anyway.
  324.  
  325.  
  326.  
  327.      Page 5                        (printed 10/23/98)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))      5555////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))
  335.  
  336.  
  337.  
  338.       HHHHoooowwww ccccaaaannnn IIII mmmmaaaakkkkeeee mmmmyyyy PPPPeeeerrrrllll pppprrrrooooggggrrrraaaammmm ttttaaaakkkkeeee lllleeeessssssss mmmmeeeemmmmoooorrrryyyy????
  339.  
  340.       When it comes    to time-space tradeoffs, Perl nearly always
  341.       prefers to throw memory at a problem.     Scalars in Perl use
  342.       more memory than strings in C, arrays    take more that,    and
  343.       hashes use even more.     While there's still a lot to be done,
  344.       recent releases have been addressing these issues.  For
  345.       example, as of 5.004,    duplicate hash keys are    shared amongst
  346.       all hashes using them, so require no reallocation.
  347.  
  348.       In some cases, using _s_u_b_s_t_r()    or _v_e_c() to simulate arrays
  349.       can be highly    beneficial.  For example, an array of a
  350.       thousand booleans will take at least 20,000 bytes of space,
  351.       but it can be    turned into one    125-byte bit vector for    a
  352.       considerable memory savings.    The standard Tie::SubstrHash
  353.       module can also help for certain types of data structure.
  354.       If you're working with specialist data structures (matrices,
  355.       for instance)    modules    that implement these in    C may use less
  356.       memory than equivalent Perl modules.
  357.  
  358.       Another thing    to try is learning whether your    Perl was
  359.       compiled with    the system malloc or with Perl's builtin
  360.       malloc.  Whichever one it is,    try using the other one    and
  361.       see whether this makes a difference.    Information about
  362.       malloc is in the _I_N_S_T_A_L_L file    in the source distribution.
  363.       You can find out whether you are using perl's    malloc by
  364.       typing perl -V:usemymalloc.
  365.  
  366.       IIIIssss iiiitttt    uuuunnnnssssaaaaffffeeee ttttoooo rrrreeeettttuuuurrrrnnnn aaaa ppppooooiiiinnnntttteeeerrrr ttttoooo llllooooccccaaaallll ddddaaaattttaaaa????
  367.  
  368.       No, Perl's garbage collection    system takes care of this.
  369.  
  370.           sub makeone {
  371.           my @a    = ( 1 .. 10 );
  372.           return \@a;
  373.           }
  374.  
  375.           for $i ( 1 .. 10 ) {
  376.           push @many, makeone();
  377.           }
  378.  
  379.           print $many[4][5], "\n";
  380.  
  381.           print "@many\n";
  382.  
  383.  
  384.       HHHHoooowwww ccccaaaannnn IIII ffffrrrreeeeeeee aaaannnn aaaarrrrrrrraaaayyyy oooorrrr hhhhaaaasssshhhh ssssoooo mmmmyyyy    pppprrrrooooggggrrrraaaammmm    sssshhhhrrrriiiinnnnkkkkssss????
  385.  
  386.       You can't.  On most operating    systems, memory    allocated to a
  387.       program can never be returned    to the system.    That's why
  388.       long-running programs    sometimes re-exec themselves.  Some
  389.       operating systems (notably, FreeBSD) allegedly reclaim large
  390.  
  391.  
  392.  
  393.      Page 6                        (printed 10/23/98)
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))      5555////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))
  401.  
  402.  
  403.  
  404.       chunks of memory that    is no longer used, but it doesn't
  405.       appear to happen with    Perl (yet).  The Mac appears to    be the
  406.       only platform    that will reliably (albeit, slowly) return
  407.       memory to the    OS.
  408.  
  409.       However, judicious use of _m_y() on your variables will    help
  410.       make sure that they go out of    scope so that Perl can free up
  411.       their    storage    for use    in other parts of your program.     A
  412.       global variable, of course, never goes out of    scope, so you
  413.       can't    get its    space automatically reclaimed, although
  414.       _u_n_d_e_f()ing and/or _d_e_l_e_t_e()ing    it will    achieve    the same
  415.       effect.  In general, memory allocation and de-allocation
  416.       isn't    something you can or should be worrying    about much in
  417.       Perl,    but even this capability (preallocation    of data    types)
  418.       is in    the works.
  419.  
  420.       HHHHoooowwww ccccaaaannnn IIII mmmmaaaakkkkeeee mmmmyyyy CCCCGGGGIIII    ssssccccrrrriiiipppptttt mmmmoooorrrreeee eeeeffffffffiiiicccciiiieeeennnntttt????
  421.  
  422.       Beyond the normal measures described to make general Perl
  423.       programs faster or smaller, a    CGI program has    additional
  424.       issues.  It may be run several times per second.  Given that
  425.       each time it runs it will need to be re-compiled and will
  426.       often    allocate a megabyte or more of system memory, this can
  427.       be a killer.    Compiling into C iiiissssnnnn''''tttt ggggooooiiiinnnngggg ttttoooo    hhhheeeellllpppp yyyyoooouuuu
  428.       because the process start-up overhead    is where the
  429.       bottleneck is.
  430.  
  431.       There    are two    popular    ways to    avoid this overhead.  One
  432.       solution involves running the    Apache HTTP server (available
  433.       from http://www.apache.org/) with either of the mod_perl or
  434.       mod_fastcgi plugin modules.
  435.  
  436.       With mod_perl    and the    Apache::Registry module    (distributed
  437.       with mod_perl), httpd    will run with an embedded Perl
  438.       interpreter which pre-compiles your script and then executes
  439.       it within the    same address space without forking.  The
  440.       Apache extension also    gives Perl access to the internal
  441.       server API, so modules written in Perl can do    just about
  442.       anything a module written in C can.  For more    on mod_perl,
  443.       see http://perl.apache.org/
  444.  
  445.       With the FCGI    module (from CPAN), a Perl executable compiled
  446.       with sfio (see the _I_N_S_T_A_L_L file in the distribution) and the
  447.       mod_fastcgi module (available    from http://www.fastcgi.com/)
  448.       each of your perl scripts becomes a permanent    CGI daemon
  449.       process.
  450.  
  451.       Both of these    solutions can have far-reaching    effects    on
  452.       your system and on the way you write your CGI    scripts, so
  453.       investigate them with    care.
  454.  
  455.       See http://www.perl.com/CPAN/modules/by-
  456.  
  457.  
  458.  
  459.      Page 7                        (printed 10/23/98)
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))      5555////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))
  467.  
  468.  
  469.  
  470.       category/15_World_Wide_Web_HTML_HTTP_CGI/ .
  471.  
  472.       A non-free, commerical product, ``The    Velocity Engine    for
  473.       Perl'', (http://www.binevolve.com/ or
  474.       http://www.binevolve.com/bine/vep) might also    be worth
  475.       looking at.  It will allow you to increase the performance
  476.       of your perl scripts,    upto 25    times faster than normal CGI
  477.       perl by running in persistent    perl mode, or 4    to 5 times
  478.       faster without any modification to your existing CGI
  479.       scripts. Fully functional evaluation copies are available
  480.       from the web site.
  481.  
  482.       HHHHoooowwww ccccaaaannnn IIII hhhhiiiiddddeeee tttthhhheeee ssssoooouuuurrrrcccceeee ffffoooorrrr    mmmmyyyy PPPPeeeerrrrllll    pppprrrrooooggggrrrraaaammmm????
  483.  
  484.       Delete it. :-) Seriously, there are a    number of (mostly
  485.       unsatisfactory) solutions with varying levels    of
  486.       ``security''.
  487.  
  488.       First    of all,    however, you _c_a_n'_t take    away read permission,
  489.       because the source code has to be readable in    order to be
  490.       compiled and interpreted.  (That doesn't mean    that a CGI
  491.       script's source is readable by people    on the web, though,
  492.       only by people with access to    the filesystem)    So you have to
  493.       leave    the permissions    at the socially    friendly 0755 level.
  494.  
  495.       Some people regard this as a security    problem.  If your
  496.       program does insecure    things,    and relies on people not
  497.       knowing how to exploit those insecurities, it    is not secure.
  498.       It is    often possible for someone to determine    the insecure
  499.       things and exploit them without viewing the source.
  500.       Security through obscurity, the name for hiding your bugs
  501.       instead of fixing them, is little security indeed.
  502.  
  503.       You can try using encryption via source filters (Filter::*
  504.       from CPAN), but crackers might be able to decrypt it.     You
  505.       can try using    the byte code compiler and interpreter
  506.       described below, but crackers    might be able to de-compile
  507.       it.  You can try using the native-code compiler described
  508.       below, but crackers might be able to disassemble it.    These
  509.       pose varying degrees of difficulty to    people wanting to get
  510.       at your code,    but none can definitively conceal it (this is
  511.       true of every    language, not just Perl).
  512.  
  513.       If you're concerned about people profiting from your code,
  514.       then the bottom line is that nothing but a restrictive
  515.       licence will give you    legal security.     License your software
  516.       and pepper it    with threatening statements like ``This    is
  517.       unpublished proprietary software of XYZ Corp.     Your access
  518.       to it    does not give you permission to    use it blah blah
  519.       blah.''  We are not lawyers, of course, so you should    see a
  520.       lawyer if you    want to    be sure    your licence's wording will
  521.       stand    up in court.
  522.  
  523.  
  524.  
  525.      Page 8                        (printed 10/23/98)
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))      5555////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))
  533.  
  534.  
  535.  
  536.       HHHHoooowwww ccccaaaannnn IIII ccccoooommmmppppiiiilllleeee mmmmyyyy PPPPeeeerrrrllll pppprrrrooooggggrrrraaaammmm iiiinnnnttttoooo bbbbyyyytttteeee ccccooooddddeeee oooorrrr CCCC????
  537.  
  538.       Malcolm Beattie has written a    multifunction backend
  539.       compiler, available from CPAN, that can do both these
  540.       things.  It is included in the perl5.005 release, but    is
  541.       still    considered experimental.  This means it's fun to play
  542.       with if you're a programmer but not really for people
  543.       looking for turn-key solutions.
  544.  
  545.       Merely compiling into    C does not in and of itself guarantee
  546.       that your code will run very much faster.  That's because
  547.       except for lucky cases where a lot of    native type
  548.       inferencing is possible, the normal Perl run time system is
  549.       still    present    and so your program will take just as long to
  550.       run and be just as big.  Most    programs save little more than
  551.       compilation time, leaving execution no more than 10-30%
  552.       faster.  A few rare programs actually    benefit    significantly
  553.       (like    several    times faster), but this    takes some tweaking of
  554.       your code.
  555.  
  556.       You'll probably be astonished    to learn that the current
  557.       version of the compiler generates a compiled form of your
  558.       script whose executable is just as big as the    original perl
  559.       executable, and then some.  That's because as    currently
  560.       written, all programs    are prepared for a full    _e_v_a_l()
  561.       statement.  You can tremendously reduce this cost by
  562.       building a shared _l_i_b_p_e_r_l._s_o library and linking against
  563.       that.     See the _I_N_S_T_A_L_L podfile in the    perl source
  564.       distribution for details.  If    you link your main perl    binary
  565.       with this, it    will make it miniscule.     For example, on one
  566.       author's system, /_u_s_r/_b_i_n/_p_e_r_l is only 11k in    size!
  567.  
  568.       In general, the compiler will    do nothing to make a Perl
  569.       program smaller, faster, more    portable, or more secure.  In
  570.       fact,    it will    usually    hurt all of those.  The    executable
  571.       will be bigger, your VM system may take longer to load the
  572.       whole    thing, the binary is fragile and hard to fix, and
  573.       compilation never stopped software piracy in the form    of
  574.       crackers, viruses, or    bootleggers.  The real advantage of
  575.       the compiler is merely packaging, and    once you see the size
  576.       of what it makes (well, unless you use a shared _l_i_b_p_e_r_l._s_o),
  577.       you'll probably want a complete Perl install anyway.
  578.  
  579.       HHHHoooowwww ccccaaaannnn IIII ggggeeeetttt    ####!!!!ppppeeeerrrrllll to work on [MS-DOS,NT,...]?
  580.  
  581.       For OS/2 just    use
  582.  
  583.           extproc perl -S -your_switches
  584.  
  585.       as the first line in *.cmd file (-S due to a bug in
  586.       cmd.exe's `extproc' handling).  For DOS one should first
  587.       invent a corresponding batch file, and codify    it in
  588.  
  589.  
  590.  
  591.      Page 9                        (printed 10/23/98)
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))      5555////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))
  599.  
  600.  
  601.  
  602.       ALTERNATIVE_SHEBANG (see the _I_N_S_T_A_L_L file in the source
  603.       distribution for more    information).
  604.  
  605.       The Win95/NT installation, when using    the ActiveState    port
  606.       of Perl, will    modify the Registry to associate the .pl
  607.       extension with the perl interpreter.    If you install another
  608.       port (Gurusaramy Sarathy's is    the recommended    Win95/NT
  609.       port), or (eventually) build your own    Win95/NT Perl using
  610.       WinGCC, then you'll have to modify the Registry yourself.
  611.  
  612.       Macintosh perl scripts will have the the appropriate Creator
  613.       and Type, so that double-clicking them will invoke the perl
  614.       application.
  615.  
  616.       _I_M_P_O_R_T_A_N_T!: Whatever you do, PLEASE don't get    frustrated,
  617.       and just throw the perl interpreter into your    cgi-bin
  618.       directory, in    order to get your scripts working for a    web
  619.       server.  This    is an EXTREMELY    big security risk.  Take the
  620.       time to figure out how to do it correctly.
  621.  
  622.       CCCCaaaannnn IIII    wwwwrrrriiiitttteeee uuuusssseeeeffffuuuullll ppppeeeerrrrllll pppprrrrooooggggrrrraaaammmmssss oooonnnn tttthhhheeee ccccoooommmmmmmmaaaannnndddd lllliiiinnnneeee????
  623.  
  624.       Yes.    Read the _p_e_r_l_r_u_n manpage for more information.    Some
  625.       examples follow.  (These assume standard Unix    shell quoting
  626.       rules.)
  627.  
  628.           #    sum first and last fields
  629.           perl -lane 'print    $F[0] +    $F[-1]'    *
  630.  
  631.           #    identify text files
  632.           perl -le 'for(@ARGV) {print if -f    && -T _}' *
  633.  
  634.           #    remove (most) comments from C program
  635.           perl -0777 -pe 's{/\*.*?\*/}{}gs'    foo.c
  636.  
  637.           #    make file a month younger than today, defeating    reaper daemons
  638.           perl -e '$X=24*60*60; utime(time(),time()    + 30 * $X,@ARGV)' *
  639.  
  640.           #    find first unused uid
  641.           perl -le '$i++ while getpwuid($i); print $i'
  642.  
  643.           #    display    reasonable manpath
  644.           echo $PATH | perl    -nl -072 -e '
  645.           s![^/+]*$!man!&&-d&&!$s{$_}++&&push@m,$_;END{print"@m"}'
  646.  
  647.       Ok, the last one was actually    an obfuscated perl entry. :-)
  648.  
  649.       WWWWhhhhyyyy ddddoooonnnn''''tttt ppppeeeerrrrllll oooonnnneeee----lllliiiinnnneeeerrrrssss wwwwoooorrrrkkkk oooonnnn mmmmyyyy DDDDOOOOSSSS////MMMMaaaacccc////VVVVMMMMSSSS ssssyyyysssstttteeeemmmm????
  650.  
  651.       The problem is usually that the command interpreters on
  652.       those    systems    have rather different ideas about quoting than
  653.       the Unix shells under    which the one-liners were created.  On
  654.  
  655.  
  656.  
  657.      Page 10                        (printed 10/23/98)
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))      5555////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))
  665.  
  666.  
  667.  
  668.       some systems,    you may    have to    change single-quotes to    double
  669.       ones,    which you must _N_O_T do on Unix or Plan9 systems.     You
  670.       might    also have to change a single % to a %%.
  671.  
  672.       For example:
  673.  
  674.           #    Unix
  675.           perl -e 'print "Hello world\n"'
  676.  
  677.           #    DOS, etc.
  678.           perl -e "print \"Hello world\n\""
  679.  
  680.           #    Mac
  681.           print "Hello world\n"
  682.            (then Run "Myscript" or Shift-Command-R)
  683.  
  684.           #    VMS
  685.           perl -e "print ""Hello world\n"""
  686.  
  687.       The problem is that none of this is reliable:    it depends on
  688.       the command interpreter.  Under Unix,    the first two often
  689.       work.    Under DOS, it's    entirely possible neither works.  If
  690.       4DOS was the command shell, you'd probably have better luck
  691.       like this:
  692.  
  693.         perl -e "print <Ctrl-x>"Hello world\n<Ctrl-x>""
  694.  
  695.       Under    the Mac, it depends which environment you are using.
  696.       The MacPerl shell, or    MPW, is    much like Unix shells in its
  697.       support for several quoting variants,    except that it makes
  698.       free use of the Mac's    non-ASCII characters as    control
  699.       characters.
  700.  
  701.       There    is no general solution to all of this.    It is a    mess,
  702.       pure and simple.  Sucks to be    away from Unix,    huh? :-)
  703.  
  704.       [Some    of this    answer was contributed by Kenneth Albanowski.]
  705.  
  706.       WWWWhhhheeeerrrreeee    ccccaaaannnn IIII lllleeeeaaaarrrrnnnn aaaabbbboooouuuutttt CCCCGGGGIIII oooorrrr WWWWeeeebbbb pppprrrrooooggggrrrraaaammmmmmmmiiiinnnngggg iiiinnnn PPPPeeeerrrrllll????
  707.  
  708.       For modules, get the CGI or LWP modules from CPAN.  For
  709.       textbooks, see the two especially dedicated to web stuff in
  710.       the question on books.  For problems and questions related
  711.       to the web, like ``Why do I get 500 Errors'' or ``Why
  712.       doesn't it run from the browser right    when it    runs fine on
  713.       the command line'', see these    sources:
  714.  
  715.           WWW Security FAQ
  716.           http://www.w3.org/Security/Faq/
  717.  
  718.           Web FAQ
  719.           http://www.boutell.com/faq/
  720.  
  721.  
  722.  
  723.      Page 11                        (printed 10/23/98)
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))      5555////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))
  731.  
  732.  
  733.  
  734.           CGI FAQ
  735.           http://www.webthing.com/page.cgi/cgifaq
  736.  
  737.           HTTP Spec
  738.           http://www.w3.org/pub/WWW/Protocols/HTTP/
  739.  
  740.           HTML Spec
  741.           http://www.w3.org/TR/REC-html40/
  742.           http://www.w3.org/pub/WWW/MarkUp/
  743.  
  744.           CGI Spec
  745.           http://www.w3.org/CGI/
  746.  
  747.           CGI Security FAQ
  748.           http://www.go2net.com/people/paulp/cgi-security/safe-cgi.txt
  749.  
  750.  
  751.       WWWWhhhheeeerrrreeee    ccccaaaannnn IIII lllleeeeaaaarrrrnnnn aaaabbbboooouuuutttt oooobbbbjjjjeeeecccctttt----oooorrrriiiieeeennnntttteeeedddd PPPPeeeerrrrllll pppprrrrooooggggrrrraaaammmmmmmmiiiinnnngggg????
  752.  
  753.       the _p_e_r_l_t_o_o_t manpage is a good place to start, and you can
  754.       use the _p_e_r_l_o_b_j manpage and the _p_e_r_l_b_o_t manpage for
  755.       reference.  Perltoot didn't come out until the 5.004
  756.       release, but you can get a copy (in pod, html, or
  757.       postscript) from http://www.perl.com/CPAN/doc/FMTEYEWTK/ .
  758.  
  759.       WWWWhhhheeeerrrreeee    ccccaaaannnn IIII lllleeeeaaaarrrrnnnn aaaabbbboooouuuutttt lllliiiinnnnkkkkiiiinnnngggg CCCC wwwwiiiitttthhhh PPPPeeeerrrrllll???? [[[[hhhh2222xxxxssss,,,, xxxxssssuuuubbbbpppppppp]]]]
  760.  
  761.       If you want to call C    from Perl, start with the _p_e_r_l_x_s_t_u_t
  762.       manpage, moving on to    the _p_e_r_l_x_s manpage, the    _x_s_u_b_p_p
  763.       manpage, and the _p_e_r_l_g_u_t_s manpage.  If you want to call Perl
  764.       from C, then read the    _p_e_r_l_e_m_b_e_d manpage, the _p_e_r_l_c_a_l_l
  765.       manpage, and the _p_e_r_l_g_u_t_s manpage.  Don't forget that    you
  766.       can learn a lot from looking at how the authors of existing
  767.       extension modules wrote their    code and solved    their
  768.       problems.
  769.  
  770.       IIII''''vvvveeee rrrreeeeaaaadddd ppppeeeerrrrlllleeeemmmmbbbbeeeedddd,,,, ppppeeeerrrrllllgggguuuuttttssss,,,, eeeettttcccc....,,,, bbbbuuuutttt IIII ccccaaaannnn''''tttt eeeemmmmbbbbeeeedddd ppppeeeerrrrllll
  771.       iiiinnnn mmmmyyyy    CCCC pppprrrrooooggggrrrraaaammmm,,,, wwwwhhhhaaaatttt    aaaammmm IIII ddddooooiiiinnnngggg wwwwrrrroooonnnngggg????
  772.  
  773.       Download the ExtUtils::Embed kit from    CPAN and run `make
  774.       test'.  If the tests pass, read the pods again and again and
  775.       again.  If they fail,    see the    _p_e_r_l_b_u_g    manpage    and send a
  776.       bugreport with the output of make test TEST_VERBOSE=1    along
  777.       with perl -V.
  778.  
  779.       WWWWhhhheeeennnn IIII ttttrrrriiiieeeedddd ttttoooo rrrruuuunnnn mmmmyyyy ssssccccrrrriiiipppptttt,,,, IIII ggggooootttt tttthhhhiiiissss mmmmeeeessssssssaaaaggggeeee.... WWWWhhhhaaaatttt ddddooooeeeessss
  780.       iiiitttt mmmmeeeeaaaannnn????
  781.  
  782.       the _p_e_r_l_d_i_a_g manpage has a complete list of perl's error
  783.       messages and warnings, with explanatory text.     You can also
  784.       use the splain program (distributed with perl) to explain
  785.       the error messages:
  786.  
  787.  
  788.  
  789.      Page 12                        (printed 10/23/98)
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))      5555////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))
  797.  
  798.  
  799.  
  800.           perl program 2>diag.out
  801.           splain [-v] [-p] diag.out
  802.  
  803.       or change your program to explain the    messages for you:
  804.  
  805.           use diagnostics;
  806.  
  807.       or
  808.  
  809.           use diagnostics -verbose;
  810.  
  811.  
  812.       WWWWhhhhaaaatttt''''ssss MMMMaaaakkkkeeeeMMMMaaaakkkkeeeerrrr????
  813.  
  814.       This module (part of the standard perl distribution) is
  815.       designed to write a Makefile for an extension    module from a
  816.       Makefile.PL.    For more information, see the
  817.       _E_x_t_U_t_i_l_s::_M_a_k_e_M_a_k_e_r manpage.
  818.  
  819.      AAAAUUUUTTTTHHHHOOOORRRR AAAANNNNDDDD    CCCCOOOOPPPPYYYYRRRRIIIIGGGGHHHHTTTT
  820.       Copyright (c)    1997, 1998 Tom Christiansen and    Nathan
  821.       Torkington.  All rights reserved.
  822.  
  823.       When included    as an integrated part of the Standard
  824.       Distribution of Perl or of its documentation (printed    or
  825.       otherwise), this works is covered under Perl's Artistic
  826.       Licence.  For    separate distributions of all or part of this
  827.       FAQ outside of that, see the _p_e_r_l_f_a_q manpage.
  828.  
  829.       Irrespective of its distribution, all    code examples here are
  830.       public domain.  You are permitted and    encouraged to use this
  831.       code and any derivatives thereof in your own programs    for
  832.       fun or for profit as you see fit.  A simple comment in the
  833.       code giving credit to    the FAQ    would be courteous but is not
  834.       required.
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.      Page 13                        (printed 10/23/98)
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.      PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))      5555////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))       PPPPEEEERRRRLLLLFFFFAAAAQQQQ3333((((1111))))
  863.  
  864.  
  865.  
  866.  
  867.  
  868.  
  869.  
  870.  
  871.  
  872.  
  873.  
  874.  
  875.  
  876.  
  877.  
  878.  
  879.  
  880.  
  881.  
  882.  
  883.  
  884.  
  885.  
  886.  
  887.  
  888.  
  889.  
  890.  
  891.  
  892.  
  893.  
  894.  
  895.  
  896.  
  897.  
  898.  
  899.  
  900.  
  901.  
  902.  
  903.  
  904.  
  905.  
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.      Page 14                        (printed 10/23/98)
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.